package com.iot.inter.controller.newinterface.trafficusage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.response.subResponse.BatchAppointMonthResponse;
import com.iot.inter.entity.newpojo.response.subResponse.common.DeviceFlow;
import com.iot.inter.entity.newpojo.response.subResponse.common.Page;
import com.iot.inter.entity.newrequest.trafficusage.batchappointmonth.request.RequestBatchAppointMonth;
import com.iot.inter.entity.newrequest.trafficusage.batchappointmonth.response.ResponseBatchAppointMonth;
import com.iot.inter.entity.newrequest.trafficusage.batchappointmonth.response.device_flow;
import com.iot.inter.interfaceprocessing.newinterface.TurnTrafficUsageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 去北京做：3.4	批量查询指定月份的流量使用情况:2.0接口
 * <p>
 * 接入方可以通过调用该接口，批量查询其拥有的所有设备信息在指定月份流量使用情况。
 * 考虑到拥有的设备数量会很多，所以本接口按照分页形式，每次查询仅获得一页设备流量使用信息，其中每页包含查询结果数量，由接入方自行定义。
 * 若要获取所有设备的流量使用情况，则需要多次调用该接口。
 *
 * @author lm
 * @date 2019/8/5
 */
@RestController
@Api(description = "3.4 批量查询指定月份的流量使用情况")
public class TrafficUsageBatchAppointMonthController {

    private static Logger logger = Logger.getLogger(TrafficUsageBatchAppointMonthController.class);

    @CrossOrigin
    @RequestMapping(value = "batchAppointMonthTrafficUsage", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.4 批量查询指定月份的流量使用情况", response = ResponseBatchAppointMonth.class)
    public String batchAppointMonthTrafficUsage(@ApiParam @RequestBody RequestBatchAppointMonth request) {

        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);
        if (requestStr != null) {

            logger.info("【批量查询指定月份的流量使用情况】请求：" + requestStr);

            RequestBatchAppointMonth requestPackage = JSONArray.parseObject(requestStr, RequestBatchAppointMonth.class);

            Integer pageNo = requestPackage.getPageNo();
            Integer pageSize = requestPackage.getPageSize();
            String month = requestPackage.getMonth();
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();

            TurnTrafficUsageService service = new TurnTrafficUsageService();
            BatchAppointMonthResponse response = service.batchAppointMonthTrafficUsage(pageNo, pageSize, month, partnerCode, secret);

            if (response != null) {
                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(response.getRetCode()) && response.getBody().getDevice_flow() != null) {

                    Page page = new Page();
                    page.setCurrentPageNo(response.getBody().getPage().getCurrentPageNo());
                    page.setPageSize(response.getBody().getPage().getPageSize());
                    page.setTotalCount(response.getBody().getPage().getTotalCount());
                    page.setTotalPages(response.getBody().getPage().getTotalPages());

                    List<device_flow> device_flowList = new ArrayList<device_flow>();

                    for (DeviceFlow d : response.getBody().getDevice_flow()) {

                        device_flow f = new device_flow();

                        f.setDeviceId(d.getDeviceId());
                        f.setFlow(d.getFlow());
                        device_flowList.add(f);
                    }

                    ResponseBatchAppointMonth res = new ResponseBatchAppointMonth();
                    res.setRetCode(response.getRetCode());
                    res.setResultInfo(response.getResultInfo());
                    res.setDevice_flow_list_num(response.getBody().getDeviceFlowListNum());
                    res.setPage(page);
                    res.setDevice_flow(device_flowList);
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(res);
                    logger.info("【批量查询指定月份的流量使用情况】成功响应：" + jsonString);
                    return jsonString;

                } else {
                    ResponseBatchAppointMonth res = new ResponseBatchAppointMonth();
                    res.setRetCode(response.getRetCode());
                    res.setResultInfo(response.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(res);
                    logger.info("【批量查询指定月份的流量使用情况】响应：" + jsonString);
                    return jsonString;
                }

            } else {
                ResponseBatchAppointMonth res = new ResponseBatchAppointMonth();
                res.setRetCode("9998");
                res.setResultInfo("响应空");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(res);
                logger.info("【批量查询指定月份的流量使用情况】响应：" + jsonString);
                return jsonString;

            }

        } else {
            ResponseBatchAppointMonth res = new ResponseBatchAppointMonth();
            res.setRetCode("9998");
            res.setResultInfo("请求为空");
            jsonString = JSON.toJSONString(res);
            logger.info("【批量查询指定月份的流量使用情况】响应：" + jsonString);
            return jsonString;

        }

    }
}
